package zmee.bid.formplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.dynamicobject.DynamicObjectType;
import kd.bos.entity.EntityMetadataCache;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.EventObject;
/**
* @author lijiawang
* @Date 2023/1/8 20:15
* @Description:资质后审获取评委
*/
public class SrcAptitudeaudiFormPlugin extends AbstractFormPlugin {
    @Override
    public void afterBindData(EventObject e) {
        IDataModel model = this.getModel();
        Object id = model.getValue("id");
        String billno = model.getValue("billno").toString();
        QFilter filter=new QFilter("billno", QCP.equals,billno);
        //获取开标准备
        DynamicObject bidopen = BusinessDataServiceHelper.loadSingle("src_bidopen", "id,judgeuser", filter.toArray());
        if(id!=null){
            //获取资质后审F7数据
            DynamicObject[] aptitudecon = BusinessDataServiceHelper.load("src_aptitudeconfig2f7", "id,scorer", new QFilter("billid",QCP.equals,id).toArray());
            for (int i = 0; i < aptitudecon.length; i++) {
                //获取资质后审的评委
                DynamicObjectCollection scorer = aptitudecon[i].getDynamicObjectCollection("scorer");
                //获取开标准备的评委
                DynamicObjectCollection judgeusers = bidopen.getDynamicObjectCollection("judgeuser");
                //获取人员类型
                DynamicObjectType type = EntityMetadataCache.getDataEntityType("bos_user");
                for (DynamicObject judgeuser:judgeusers) {
                    String fbasedataid = judgeuser.getString("fbasedataid.id");
                    DynamicObject user = BusinessDataServiceHelper.loadSingle(fbasedataid, type);
                    //添加开标准备的评委
                    DynamicObject base=new DynamicObject(scorer.getDynamicObjectType());
                    base.set("fbasedataid",user);
                    if (scorer.size()>0) {
                        boolean taid=false;
                        for (int j = 0; j < scorer.size(); j++) {
                            String based = scorer.get(j).getString("fbasedataid.id");
                            if(user.getPkValue().toString().equals(based)){
                                taid=true;
                            }
                        }
                        if(!taid){
                            scorer.add(base);
                        }
                    }else{
                        scorer.add(base);
                    }
                }

            }
            SaveServiceHelper.save(aptitudecon);
        }

    }
}
